//
//  CDisItemView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-9.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CDisItemView.h"
#import <QuartzCore/QuartzCore.h>

//================================================================================================================

@implementation CDisContent

@synthesize m_Text;
@synthesize m_Cross;
@synthesize m_Border;

-(id)initWithFrame:(CGRect)frame Text:(NSString*)text
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.autoresizesSubviews = YES;
        
        /*m_Cross = [[UIImageView alloc] initWithFrame:CGRectMake(10, 1, 17, 9)];
        m_Cross.image = [UIImage imageNamed:@"cross.png"];
        m_Cross.backgroundColor = [UIColor clearColor];
        [self addSubview:m_Cross];
        [m_Cross release];
        
        m_Border = [[UIView alloc] initWithFrame:CGRectMake(0, m_Cross.frame.size.height, frame.size.width, frame.size.height-m_Cross.frame.size.height)];
        m_Border.layer.borderWidth = 1;
        m_Border.layer.cornerRadius= 5;
        m_Border.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        //m_Border.backgroundColor = [UIColor colorWithRed:0.976 green:0.980 blue:0.960 alpha:1.0];
        m_Border.clipsToBounds = YES;
        [self addSubview:m_Border];
        [m_Border release];
        
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = CGRectMake(1, 1, m_Border.frame.size.width-2, m_Border.frame.size.height-2);
        gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.992 green:1.0 blue:0.980 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:0.964 green:0.968 blue:0.949 alpha:1.0] CGColor], nil];
        [m_Border.layer addSublayer:gradient];*/
        
        m_Text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        m_Text.backgroundColor = [UIColor clearColor];
        m_Text.font = [UIFont systemFontOfSize:12];//DIS_FONTSIZE
        m_Text.textColor = [UIColor whiteColor];
        m_Text.text = text;
        [self addSubview:m_Text];
        [m_Text release];
        
        //[self bringSubviewToFront:m_Cross];
    }
    
    return self;
}

-(void)layoutSubviews
{
    CGRect frame = self.frame;
    
    //m_Cross.frame  = CGRectMake(10, 1, 17, 9);
    //m_Border.frame = CGRectMake(0, m_Cross.frame.size.height, frame.size.width, frame.size.height-m_Cross.frame.size.height);
    m_Text.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    //[self bringSubviewToFront:m_Cross];
}

+(CGFloat)GetDicContentHeight:(NSString*)text Width:(CGFloat)width Font:(UIFont*)font
{
    if (text && [text length] > 0)
    {
        CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
        
        return size.height+25;
    }
    
    return 0;
}

@end


//================================================================================================================

@implementation CDisItemView

@synthesize m_Imgae;
@synthesize m_Name;
@synthesize m_Content;

- (id)initWithFrame:(CGRect)frame Name:(NSString*)name Content:(NSString*)content Time:(NSString*)time IconDic:(int)dic
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.autoresizesSubviews = YES;
        
        UIImageView *line = nil;;
        
        //left
        if (0 == dic)
        {
            line = [[UIImageView alloc] initWithFrame:CGRectMake(45, 0, frame.size.width-45, 21)];
            line.image = [UIImage imageNamed:@"d-1.png"];
            [self addSubview:line];
            [line release];
            
            line = [[UIImageView alloc] initWithFrame:CGRectMake(45, 21, frame.size.width-45, frame.size.height-30)];
            line.image = [UIImage imageNamed:@"d-2.png"];
            [self addSubview:line];
            [line release];
            
            line = [[UIImageView alloc] initWithFrame:CGRectMake(45, line.frame.origin.y+line.frame.size.height, frame.size.width-45, 9)];
            line.image = [UIImage imageNamed:@"d-3.png"];
            [self addSubview:line];
            [line release];
            
            m_Name = [[UILabel alloc] initWithFrame:CGRectMake(65, 5, frame.size.width-65, 15)];
            m_Name.textColor = [UIColor brownColor];
            m_Name.backgroundColor = [UIColor clearColor];
            m_Name.font = [UIFont systemFontOfSize:12];
            m_Name.text = name;
            [self addSubview:m_Name];
            [m_Name release];
            
            m_Content = [[CDisContent alloc] initWithFrame:CGRectMake(65, m_Name.frame.size.height, frame.size.width-65, frame.size.height-m_Name.frame.size.height) Text:content];
            [self addSubview:m_Content];
            [m_Content release];
            
            UIView *border = [[UIView alloc] initWithFrame:CGRectMake(0, 3, 46, 46)];
            border.backgroundColor = [UIColor blackColor];
            border.layer.cornerRadius = 23;
            border.layer.shadowOffset = CGSizeMake(3, 3);
            border.layer.shadowColor  = [[UIColor blackColor] CGColor];
            border.layer.shadowOpacity= 0.8;
            [self addSubview:border];
            [border release];
            
            m_Imgae = [[UIImageView alloc] initWithFrame:CGRectMake(0, 3, 46, 46)];
            m_Imgae.layer.masksToBounds = YES;
            m_Imgae.layer.cornerRadius = 23;
            m_Imgae.image = [UIImage imageNamed:@"icon.png"];
            [self addSubview:m_Imgae];
            [m_Imgae release];
        }
        else 
        {
            line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width-45, 21)];
            line.image = [UIImage imageNamed:@"b-1.png"];
            [self addSubview:line];
            [line release];
            
            line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 21, frame.size.width-45, frame.size.height-30)];
            line.image = [UIImage imageNamed:@"b-2.png"];
            [self addSubview:line];
            [line release];
            
            line = [[UIImageView alloc] initWithFrame:CGRectMake(0, line.frame.origin.y+line.frame.size.height, frame.size.width-45, 9)];
            line.image = [UIImage imageNamed:@"b-3.png"];
            [self addSubview:line];
            [line release];
            
            m_Name = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, frame.size.width-58, 15)];
            m_Name.textColor = [UIColor brownColor];
            m_Name.backgroundColor = [UIColor clearColor];
            m_Name.font = [UIFont systemFontOfSize:12];
            m_Name.text = name;
            [self addSubview:m_Name];
            [m_Name release];
            
            m_Content = [[CDisContent alloc] initWithFrame:CGRectMake(10, m_Name.frame.size.height, frame.size.width-58, frame.size.height-m_Name.frame.size.height) Text:content];
            [self addSubview:m_Content];
            [m_Content release];
            
            UIView *border = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-46, 3, 46, 46)];
            border.backgroundColor = [UIColor blackColor];
            border.layer.cornerRadius = 23;
            border.layer.shadowOffset = CGSizeMake(3, 3);
            border.layer.shadowColor  = [[UIColor blackColor] CGColor];
            border.layer.shadowOpacity= 0.8;
            [self addSubview:border];
            [border release];
            
            m_Imgae = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-46, 3, 46, 46)];
            m_Imgae.layer.masksToBounds = YES;
            m_Imgae.layer.cornerRadius = 23;
            m_Imgae.image = [UIImage imageNamed:@"icon.png"];
            [self addSubview:m_Imgae];
            [m_Imgae release];
        }
    }
    
    return self;
}

//del
- (id)initWithFrame:(CGRect)frame IconDic:(int)dic
{
    self = [super initWithFrame:frame];
    /*if (self)
    {
        self.backgroundColor = [UIColor clearColor];
    
        //left
        if (0 == dic)
        {
            m_Name = [[UILabel alloc] initWithFrame:CGRectMake(53, 0, frame.size.width-53, 0)];
            m_Name.textColor = [UIColor brownColor];
            m_Name.backgroundColor = [UIColor clearColor];
            m_Name.enabled = NO;
            [self addSubview:m_Name];
            [m_Name release];
            
            m_Content = [[CDisContent alloc] initWithFrame:CGRectMake(53, m_Name.frame.size.height, frame.size.width-53, frame.size.height-m_Name.frame.size.height) Text:nil];
            [self addSubview:m_Content];
            [m_Content release];
            
            m_Imgae = [[UIImageView alloc] initWithFrame:CGRectMake(0, 3, 52, 52)];
            m_Imgae.layer.masksToBounds = YES;
            m_Imgae.layer.cornerRadius = 26;
            m_Imgae.image = [UIImage imageNamed:@"icon.png"];
            [self addSubview:m_Imgae];
            [m_Imgae release];
        }
        else 
        {
            m_Name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width-58, 0)];
            m_Name.textColor = [UIColor brownColor];
            m_Name.backgroundColor = [UIColor clearColor];
            m_Name.enabled = NO;
            [self addSubview:m_Name];
            [m_Name release];
            
            m_Content = [[CDisContent alloc] initWithFrame:CGRectMake(0, m_Name.frame.size.height, frame.size.width-58, frame.size.height-m_Name.frame.size.height) Text:nil];
            [self addSubview:m_Content];
            [m_Content release];
            
            m_Imgae = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-52, 3, 52, 52)];
            m_Imgae.layer.masksToBounds = YES;
            m_Imgae.layer.cornerRadius = 26;
            m_Imgae.image = [UIImage imageNamed:@"icon.png"];
            [self addSubview:m_Imgae];
            [m_Imgae release];
        }
    }
    
    m_Imgae.alpha = 0.5;*/
    
    return self;
}

-(void)layoutSubviews
{
    CGRect frame = self.frame;
    
    m_Name.frame = CGRectMake(m_Name.frame.origin.x, m_Name.frame.origin.y, frame.size.width-65, 15);
    m_Content.frame = CGRectMake(m_Content.frame.origin.x, m_Name.frame.size.height, frame.size.width-65, frame.size.height-m_Name.frame.size.height);
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [textView resignFirstResponder];
    
    return NO;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
